//package com.ling.pl;
//
//import com.ling.pl.core.commons.exception.BusinessException;
//import com.ling.pl.core.commons.springmvc.ResposeInfo;
//import feign.FeignException;
//import feign.Response;
//import feign.codec.Decoder;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//
//import java.io.IOException;
//import java.lang.reflect.ParameterizedType;
//import java.lang.reflect.Type;
//import java.util.LinkedList;
//
///**
// * Created by bo.wang on 2017/4/9.
// */
//public class LingResponseEntityDecoder implements Decoder {
//
//    private Decoder decoder;
//
//    public LingResponseEntityDecoder(Decoder decoder) {
//        this.decoder = decoder;
//    }
//
//    @Override
//    public Object decode(final Response response, Type type) throws IOException,
//            FeignException {
//
//        if (isParameterizeHttpEntity(type)) {
//            type = ((ParameterizedType) type).getActualTypeArguments()[0];
//            Object decodedObject = decoder.decode(response, type);
//
//            return createResponse(decodedObject, response);
//        } else if (isHttpEntity(type)) {
//            return createResponse(null, response);
//        } else {
////            try {
////                Object object = decoder.decode(response, ResposeInfo.class);
////                ResposeInfo resposeInfo = (ResposeInfo) object;
////                if (resposeInfo.isSuccess()) {
////                    return resposeInfo.getData();
////                } else {
////                    throw new BusinessException(resposeInfo.getMessage());
////                }
////            } catch (BusinessException e) {
////                throw new BusinessException(e.getMessage());
////            } catch (Exception e) {
//                return decoder.decode(response, type);
////            }
//        }
//    }
//
//    private boolean isParameterizeHttpEntity(Type type) {
//        if (type instanceof ParameterizedType) {
//            return isHttpEntity(((ParameterizedType) type).getRawType());
//        }
//        return false;
//    }
//
//    private boolean isHttpEntity(Type type) {
//        if (type instanceof Class) {
//            Class c = (Class) type;
//            return HttpEntity.class.isAssignableFrom(c);
//        }
//        return false;
//    }
//
//    @SuppressWarnings("unchecked")
//    private <T> ResponseEntity<T> createResponse(Object instance, Response response) {
//
//        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
//        for (String key : response.headers().keySet()) {
//            headers.put(key, new LinkedList<>(response.headers().get(key)));
//        }
//
//        return new ResponseEntity<>((T) instance, headers, HttpStatus.valueOf(response
//                .status()));
//    }
//}
